/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.global;

import static cn.ac.iie.di.gwxs.test.id2Provinces;
import cn.ac.iie.di.ipparser.RangeSearch;
import cn.ac.iie.di.tools.Configuration;
import cn.ac.iie.di.tools.JedisConnectionHandler;
import cn.ac.iie.di.tools.TimeFormat;
import cn.ac.iie.di.tools.UnicodeUtils;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lenovo-pc
 */
public class GlobalParas {

    static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(GlobalParas.class.getName());
    public static ArrayBlockingQueue<String> gwxQueue = new ArrayBlockingQueue(1600);
    public static ArrayBlockingQueue<HashMap<Long, Integer>> mapQueue = new ArrayBlockingQueue(500);
    public static ArrayBlockingQueue<String> untarQueue = new ArrayBlockingQueue(100);
    public static ArrayBlockingQueue<String> fileQueue = new ArrayBlockingQueue(500);
    
    public static AtomicInteger videoCount = new AtomicInteger(0);
    public static AtomicInteger voiceCount = new AtomicInteger(0);
    public static AtomicInteger imgCount = new AtomicInteger(0);
    public static AtomicInteger translateCount = new AtomicInteger(0);

    public static Integer HashCode = null;
    public static Integer EndCode = null;
    public static Integer SendNum = null;

    public static String FTP_IPPORT = null;
    public static String FTP_NAMEPWD = null;
    public static String FTP_PATH = null;

    public static List<String> CONT = new ArrayList<String>();
    public static String IP_PORT = null;
    public static String MQ_GWX = null;
    public static String MQ_OCC = null;
    public static String TYPE_NEWS = null;
    public static String TYPE_OCC = null;

    public static String ORI_GWX = null;
    public static String TMP_GWX = null;
    public static String TMP_OCC = null;

    public static String DES_GWX = null;
    public static String DES_CNT = null;

    public static String FILE_NEWS = null;

    public static RangeSearch rs1 = null;

    public static HashMap<String, Integer> id2Provinces = new HashMap();


    static {
        try {
            //ftp  ip端口号
            FTP_IPPORT = LoadProperties("FTP_IPPORT");
            FTP_NAMEPWD = LoadProperties("FTP_NAMEPWD");
            FTP_PATH = LoadProperties("FTP_PATH");
            //MQ端口号
            IP_PORT = LoadProperties("IP_PORT");
            //消息队列名称
            MQ_GWX = LoadProperties("MQ_NEWS");
            MQ_OCC = LoadProperties("MQ_CNT");

            //解析字段
            TYPE_NEWS = LoadProperties("TYPE_NEWS");
            TYPE_OCC = LoadProperties("TYPE_CNT");
            HashCode = Integer.valueOf(LoadProperties("HashCode"));
            EndCode = Integer.valueOf(LoadProperties("EndCode"));
            SendNum = Integer.valueOf(LoadProperties("SendNum"));

            //需要解压的文件夹
            ORI_GWX = LoadProperties("ORI_NEWS");
            //解析文件夹
            TMP_GWX = LoadProperties("TMP_NEWS");
            TMP_OCC = LoadProperties("TMP_CNT");

            //最后存放原始文件的文件夹
            DES_GWX = LoadProperties("DES_NEWS");
            DES_CNT = LoadProperties("DES_CNT");

            //不同的文件夹挪走文件处理不一致
            FILE_NEWS = LoadProperties("FILE_NEWS");
            String encoding = "UTF-8";

            readId2Province();
            logger.info("readIP2Province over");
            logger.info("xxxxxxxxxxxxxxxx" + GlobalParas.id2Provinces.toString());
            try {
                ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(ClassLoader.getSystemClassLoader().getResource("IPGeoLocator.dat").getFile())));
                rs1 = (RangeSearch) in.readObject();
            } catch (IOException ex) {
                logger.error(ex, ex);
                System.exit(0);
            } catch (ClassNotFoundException ex) {
                logger.error(ex, ex);
                System.exit(0);
            }

            File file = new File(ClassLoader.getSystemClassLoader().getResource("cont.properties").getFile());
            if (file.isFile() && file.exists()) {
                InputStreamReader read = null;
                try {
                    //判断文件是否存在
                    read = new InputStreamReader(new FileInputStream(file), encoding); //考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while ((lineTxt = bufferedReader.readLine()) != null) {
                        CONT.add(UnicodeUtils.unicode2String(lineTxt));
                    }
                    read.close();
                } catch (Exception ex) {
                    Logger.getLogger(GlobalParas.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        read.close();
                    } catch (IOException ex) {
                        Logger.getLogger(GlobalParas.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(GlobalParas.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static String LoadProperties(String str) {
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource("config.properties").getFile());
        return conf.getString(str, "");
    }

    public static HashMap<String, Object> Map() {
        HashMap map = new HashMap<String, Object>();
        for (String columntype : GlobalParas.LoadProperties("MAP").split("\\|")) {
            map.put(columntype.split("[:]")[0].toLowerCase(), columntype.split("[:]")[1].toLowerCase());
        }
        return map;
    }

    public static void readId2Province() throws IOException {
//        BufferedReader br = new BufferedReader(new FileReader(ClassLoader.getSystemClassLoader().getResource("log4j.properties").getFile()));
        BufferedReader br = null;
        try {
            File file = new File(ClassLoader.getSystemClassLoader().getResource("ip2province").getFile());
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] id2Province = line.split("[|]");
                GlobalParas.id2Provinces.put(id2Province[0].trim(), Integer.parseInt(id2Province[1].trim()));
            }
            logger.info("GlobalParas.id2Provinces size" + GlobalParas.id2Provinces.size());
        } catch (Exception e) {
            logger.error(e, e);
            System.exit(0);
        } finally {
            br.close();
        }
    }
}
